package com.sky.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.sky.utils.ThreadLocalUtil;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
public class AutoFillConfig implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        Object value = ThreadLocalUtil.getValue();

        metaObject.setValue("createUser", value);
        metaObject.setValue("updateUser", value);
        metaObject.setValue("createTime", LocalDateTime.now());
        metaObject.setValue("updateTime", LocalDateTime.now());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        Object value = ThreadLocalUtil.getValue();
        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("updateUser",value);
    }
}
